package com.ethink.order.manager;

import cn.hutool.core.util.ObjectUtil;
import com.ethink.framework.common.constant.CommonRespCode;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.order.api.constant.OrderRespCode;
import com.ethink.order.api.enums.SaleDemandDetailOrderStatusEnum;
import com.ethink.order.api.info.PurchaseOrderDetailInfo;
import com.ethink.order.api.request.OAPassRequest;
import com.ethink.order.api.request.SaleDemandOrderLockRequest;
import com.ethink.order.api.request.SaleDemandOrderUpdateConfirmRequest;
import com.ethink.order.api.request.SaleLockRequest;
import com.ethink.order.api.response.SaleConfirmPurchaseResponse;
import com.ethink.order.common.entity.SaleDemandOrderDetail;
import com.ethink.order.common.mapper.SaleDemandOrderDetailMapper;
import com.ethink.order.common.mapper.SaleDemandOrderMapper;
import com.ethink.order.service.PurchaseOrderDetailService;
import com.ethink.order.service.SaleDemandOrderDetailService;
import com.ethink.order.service.SaleDemandOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author default
 * @version 1.0.0
 * @ClassName SaleDemandManager.java
 * @Description
 * @createTime 2022年11月07日 11:21:00
 */
@Component
public class SaleDemandManager {


    @Autowired
    private SaleDemandOrderDetailMapper saleDemandOrderDetailMapper;

    @Autowired
    private PurchaseOrderDetailService purchaseOrderDetailService;

    @Autowired
    private SaleDemandOrderService saleDemandOrderService;

    @Autowired
    private SaleDemandOrderDetailService saleDemandOrderDetailService;

    @Autowired
    private SaleDemandOrderMapper saleDemandOrderMapper;


    @Transactional(rollbackFor = Exception.class)
    public void lock(SaleDemandOrderLockRequest request) {
        SaleDemandOrderDetail saleDemandOrderDetail = saleDemandOrderDetailMapper.selectById(request.getId());
        int sum = request.getItem().stream().mapToInt(PurchaseOrderDetailInfo::getLockOrderQty).sum();
        for (PurchaseOrderDetailInfo purchaseOrderDetailInfo : request.getItem()) {
            if (purchaseOrderDetailInfo.getLockOrderQty() > purchaseOrderDetailInfo.getAvailableQty()) {
                throw new BusinessException(OrderRespCode.NUMBER_OF_MORE_AVAILABLE);
            }
        }
        if (sum > saleDemandOrderDetail.getDemandAffirmQty()) {
            throw new BusinessException(OrderRespCode.NUMBER_OF_MORE);
        }
        List<SaleLockRequest> saleLockRequests = new ArrayList<>();
        SaleLockRequest saleLockRequest;
        //计算采购平均价 用于计算毛利率
        BigDecimal purchaseTotalPrice = BigDecimal.ZERO;
        for (PurchaseOrderDetailInfo info : request.getItem()) {
            saleLockRequest = new SaleLockRequest();
            saleLockRequest.setId(info.getId());
            saleLockRequest.setLockOrderQty(info.getLockOrderQty());
            saleLockRequests.add(saleLockRequest);
            purchaseTotalPrice = purchaseTotalPrice.add(info.getPurchasePriceBb().multiply(new BigDecimal(info.getLockOrderQty())));
        }
        BigDecimal averagePurchasePrice;
        if (sum == 0) {
            averagePurchasePrice = BigDecimal.ZERO;
        } else {
            averagePurchasePrice = purchaseTotalPrice.divide(new BigDecimal(sum), 6, BigDecimal.ROUND_HALF_DOWN);
        }
        request.setAveragePurchasePrice(averagePurchasePrice);
        purchaseOrderDetailService.saleLock(saleLockRequests);
        saleDemandOrderDetailService.lock(request);
    }

    @Transactional(rollbackFor = Exception.class)
    public void unlock(Long id) {
        SaleDemandOrderDetail saleDemandOrderDetail = saleDemandOrderDetailMapper.selectById(id);
        if (!SaleDemandDetailOrderStatusEnum.S2.getIntValue().equals(saleDemandOrderDetail.getDetailOrderStatus())) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR.getCode(), "非法请求");
        }

        purchaseOrderDetailService.saleUnlock(saleDemandOrderDetail.getDetailOrderNo());
        saleDemandOrderDetailService.unlock(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public SaleConfirmPurchaseResponse oaPass(OAPassRequest request) {
        SaleConfirmPurchaseResponse response = new SaleConfirmPurchaseResponse();
        SaleDemandOrderDetail saleDemandOrderDetail = saleDemandOrderService.oaPass(request);
        List<PurchaseOrderDetailInfo> winning = purchaseOrderDetailService.winning(saleDemandOrderDetail.getDetailOrderNo());
        response.setList(winning);
        response.setOrderNo(saleDemandOrderDetail.getOrderNo());
        return response;
    }

    public Integer demandConfirmUpdate(SaleDemandOrderUpdateConfirmRequest request) {
        SaleDemandOrderDetail demandOrderDetail = saleDemandOrderDetailMapper.selectById(request.getId());
        if (ObjectUtil.isEmpty(demandOrderDetail)) {
            return 0;
        }
        List<PurchaseOrderDetailInfo> detailInfos = purchaseOrderDetailService.queryPurchaseDetailOrderByDetailOrderNo(demandOrderDetail.getDetailOrderNo());
        if (detailInfos.size() > 0) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR.getCode(), "采购已提报,无法更改");
        }
        demandOrderDetail.setDemandAffirmQty(request.getDemandAffirmQty());
        return saleDemandOrderDetailMapper.updateById(demandOrderDetail);
    }
}
